package zclients

import (
	"context"
	"github.com/pkg/errors"
	"github.com/uber/tchannel-go/thrift"
	"go.didapinche.com/goapi/user_department_service_api"
	"go.didapinche.com/zeus-go/v2"
	"go.didapinche.com/zeus-go/v2/client"
	"time"
)

func NewDepartmentService(z *zeus.Zeus) (user_department_service_api.TChanDepartmentService, error) {
	c, err := client.New(z, "DepartmentService")
	if err != nil {
		//return nil, errors.Wrap(err, "create zeus LimosService error")
		return nil, nil
	}
	tc := user_department_service_api.NewTChanDepartmentServiceClient(c)
	return tc, nil
}

//获得一个groupID的所有子groupID包含自己
func GetNodeIDs(ctx context.Context, department user_department_service_api.TChanDepartmentService, groupID string) ([]string, error) {
	ctx2, _ := context.WithTimeout(ctx, 5*time.Second)
	node, err := department.FindNodeById(thrift.Wrap(ctx2), groupID, true, true)
	if err != nil {
		return nil, errors.Wrap(err, "call uicService.FindGroupsOfDevelopment error")
	}
	node = GetMapByNode(node, groupID)
	m := GetNodeIDMap(node)
	nodeIDs := make([]string, 0)
	for k, _ := range m {
		nodeIDs = append(nodeIDs, k)
	}
	return nodeIDs, nil
}

//找到对应greouID的node
func GetMapByNode(node *user_department_service_api.DepartmentTreeNode, groupID string) *user_department_service_api.DepartmentTreeNode {
	if node == nil {
		return nil
	}
	if node.ID == groupID {
		return node
	}
	for _, n := range node.ChildrenNode {
		node2 := GetMapByNode(n, groupID)
		if node2 != nil {
			return node2
		}
	}
	return nil
}

//找到对应node的所有NodeID
func GetNodeIDMap(node *user_department_service_api.DepartmentTreeNode) map[string]int {
	if node == nil {
		return nil
	}
	nodeIDs := make(map[string]int, 0)
	nodeIDs[node.ID] = 0
	for _, n := range node.ChildrenNode {
		for k, _ := range GetNodeIDMap(n) {
			nodeIDs[k] = 0
		}
	}
	return nodeIDs
}

//找到nodeId:nodeName的结构
func GetNodeNameMap(node *user_department_service_api.DepartmentTreeNode) map[string]string {
	if node == nil {
		return nil
	}
	nodeIDs := make(map[string]string, 0)
	nodeIDs[node.ID] = node.Name
	for _, n := range node.ChildrenNode {
		nodeIDs1 := GetNodeNameMap(n)
		if nodeIDs1 != nil {
			for k, v := range nodeIDs1 {
				nodeIDs[k] = v
			}
		}
	}
	return nodeIDs
}

//找到node最深层级
func GetNodeLevel(node *user_department_service_api.DepartmentTreeNode, i int) int {
	if node == nil {
		return i
	}
	if len(node.ChildrenNode) == 0 {
		return i
	}
	arr := make([]int, 0)
	i++
	for _, n := range node.ChildrenNode {
		arr = append(arr, GetNodeLevel(n, i))
	}
	var max int
	for _, val := range arr {
		if val > max {
			max = val
		}
	}
	return max
}

//获得子对应父的层级关系(部门名字)
func GetMap(node *user_department_service_api.DepartmentTreeNode) map[string]string {
	tmpMap := make(map[string]*user_department_service_api.DepartmentTreeNode, 0)
	result := make(map[string]string)
	//每层节点
	tmpSlice := make([]string, 0)

	tmpSlice = append(tmpSlice, node.Name)
	tmpMap[node.Name] = node
	result[node.Name] = ""

	for len(tmpSlice) != 0 {
		tmp := make([]string, 0)
		for _, tmpNodeID := range tmpSlice {
			nodes := tmpMap[tmpNodeID].ChildrenNode
			if nil != nodes && len(nodes) > 0 {
				for _, tmpNode := range nodes {
					tmp = append(tmp, tmpNode.Name)
					tmpMap[tmpNode.Name] = tmpNode
					result[tmpNode.Name] = tmpNodeID
				}
			}
		}
		tmpSlice = tmp
	}

	return result
}

//获得子对应父的层级关系(部门名字Id)
func GetMapID(node *user_department_service_api.DepartmentTreeNode) map[string]string {
	tmpMap := make(map[string]*user_department_service_api.DepartmentTreeNode, 0)
	result := make(map[string]string)
	//每层节点
	tmpSlice := make([]string, 0)

	tmpSlice = append(tmpSlice, node.ID)
	tmpMap[node.ID] = node
	result[node.ID] = ""

	for len(tmpSlice) != 0 {
		tmp := make([]string, 0)
		for _, tmpNodeID := range tmpSlice {
			nodes := tmpMap[tmpNodeID].ChildrenNode
			if nil != nodes && len(nodes) > 0 {
				for _, tmpNode := range nodes {
					tmp = append(tmp, tmpNode.ID)
					tmpMap[tmpNode.ID] = tmpNode
					result[tmpNode.ID] = tmpNodeID
				}
			}
		}
		tmpSlice = tmp
	}

	return result
}
